package tk.mybatis.springboot.share.util;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;
import tk.mybatis.mapper.util.StringUtil;

public class WechatUtil {

	/** 公众号的APPID，现在是开发者的 */

	//	public static String Appid = "wx8f6d93bde767dd47"; //个人调试
	public static String Appid = "wx19f8ae6bf5557e93"; //测试号
	/** 公众号的AppSecret(应用密钥) */
	//	public static String AppSecret = "285e16ca75d38211c1805d152966f14f";//个人调试
	public static String AppSecret = "9a41c95e76020ae886a2a02754eb0aa2";//测试号

	/** 公众号的(消息加解密密钥) */
	public static String EncodingAESKey = "399kXEoS9R5piZ9fhCuO28aMLpJlaz6PF5RtQc67koz";

	public static final String base_url = "https://api.weixin.qq.com";
	/** 微信地址 */
	public static String url = "https://api.weixin.qq.com";
	/** 微信验证 */
	public static String token = "t_yihong";

	/** 缓存命名access_token微信api授权 */
	public static final String cache_access_token = "mp_wechat_access_token";
	/** 缓存命名jsapi_ticket微信js-sdk授权 */
	public static final String cache_jsapi_ticket = "mp_wehcat_jsapi_ticket";

	/** 微信模板：运单状态通知 */
	public static final String weichat_message_goods_state = "weichat_message_goods_state";
	/** 微信模板：认证状态通知 */
	public static final String wechat_message_approve_state = "wechat_message_approve_state";
	/** 微信模板：货源修改通知 */
	public static final String weichat_owner_message_goods_state = "weichat_owner_message_goods_state";
	/** 得到微信的标识 */
	public static String getAccess_token() {
		//		String token = "v6PSU5CHAi5BvyUAHfPAICqloH2TybXotHDk6CgYqGgMywjen8YbTx7CLHlsPZil377o1q3dFaxzbXDYNPTJNuFtXwC2B2aabevG1cak4nQDu67Q9IPE453ZhYamAcbMJGWaAAAZWM";
		//
		//		if (1 < 2) {
		//			return token;
		//		}
		//先从缓存中取找，如果是有，直接用缓存中的值
		String access_token;

		Map<String, String> t_map = new HashMap<>();
		//固定
		t_map.put("grant_type", "client_credential");
		t_map.put("appid", Appid);
		t_map.put("secret", AppSecret);

		String str = HttpUtil.sendPost(url + "/cgi-bin/token", t_map);
		System.out.println(str);
		//返回的对象
		JSONObject json = JSONObject.fromObject(str);

		//判断是否存在
		access_token = (String) json.get("access_token");
		if (StringUtil.isNotEmpty(access_token)) {
			int expires_in = json.getInt("expires_in");
		} else {
			return null;
		}

		return access_token;
	}


	public static void main(String[] args) {
		Map<String, String> t_map = new HashMap<>();
		//固定
		t_map.put("grant_type", "client_credential");
		t_map.put("appid", "wx04568703f5cdd0bf");
		t_map.put("secret", "f5ca0ab6900466d8524946f2491a5b01");

		String str = HttpUtil.sendPost(url + "/cgi-bin/token", t_map);
		System.out.println(str);
		//返回的对象
		JSONObject json = JSONObject.fromObject(str);

		//判断是否存在
		String access_token = (String) json.get("access_token");
		if (StringUtil.isNotEmpty(access_token)) {
			int expires_in = json.getInt("expires_in");
			//保存到缓存中取
		} else {
		}
		System.out.println(access_token);
	}
}

